/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package demo916;

/**
 *
 * @author Administrator
 */
import java.util.Scanner;

/**
 *
 * @author Administrator
 */
import java.util.Scanner;

/**
 *
 * @author Administrator
 */
public class MainC {

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
       
        int year, month, day, t;
        int mm[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
        t = cin.nextInt();
        while (t-->0) {
            year = cin.nextInt();
            month = cin.nextInt();
            day = cin.nextInt();
            day += mm[month - 1];
            if (isLeapYear(year) && month > 2) {
                System.out.println(day + 1);
            } else {
                System.out.println(day);
            }
        }

    }
    public static boolean isLeapYear(int year) {
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400) == 0) {
            return true;
        }
        return false;
    }
}
